import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'


import moment from 'moment/moment'
Vue.filter('moment', function (value, formatString) {
    formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
 return moment(value).format(formatString);
});

import './vant/vant'
import request from './utils/request'
Vue.prototype.$axios = request

Vue.config.productionTip = false



//  混入

// main.js是我们单页面应用的入口  不管你在哪个页面刷新的 从哪个页面进来的 


let token = localStorage.getItem('xl-token')
let tokenExpired = localStorage.getItem('xl-tokenExpired') //明天过期
let uid = localStorage.getItem('xl-uid')

let now = new Date().getTime()
if (token && uid && now < tokenExpired) {
  //  OK的情况  

  // 代表我们一段时间内登录过  免登录

  // 1. 从本地拿  可能是老数据   
  // 2. 从服务器拿  

  request.post('/user/getuserInfo', {
    token: token
  })
    .then(res => {
      // console.log(res)

      let {
        userInfo
      } = res;
        
      localStorage.getItem('xl-userInfo',JSON.stringify(userInfo))

      new Vue({
        router,
        store,
        data: {
          token,
          uid,
          userInfo
        },
        render: h => h(App)
      }).$mount('#app')



    })


} else {

  //  意思是原来没登录 或者token过期了 
  //  需要重新登
  localStorage.removeItem('xl-token')
  localStorage.removeItem('xl-tokenExpired')
  localStorage.removeItem('xl-uid')
  localStorage.removeItem('xl-userInfo')

  new Vue({
    router,
    store,
    data: {
      token: '',
      uid: '',
      userInfo: {}
    },
    render: h => h(App)
  }).$mount('#app')


}





// new Vue({
//   router,
//   store,
//   render: h => h(App)
// }).$mount('#app')
